package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AddressBookMapper;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.AgencyCertificationAuditService;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements AgencyCertificationAuditService {

    @Resource
    private AgencyCertificationAuditMapper agencyCertificationAuditMapper;

    /**
     * 提交认证信息
     * @param
     */
    @Override
    public void addAuthentication(AgencyCertificationAuditResDTO AgencyCertificationAuditResDTO) {
        AgencyCertificationAudit agencyCertificationAudit = new AgencyCertificationAudit();
        BeanUtil.copyProperties(AgencyCertificationAuditResDTO, agencyCertificationAudit);
        // createTime;
        agencyCertificationAudit.setCreateTime(LocalDateTime.now());
        // updateTime;
        agencyCertificationAudit.setUpdateTime(LocalDateTime.now());
        //  certificationStatus;
        agencyCertificationAudit.setAuditStatus(CertificationStatusEnum.INIT.getStatus());
        //  certificationStatus;
        agencyCertificationAudit.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());

        this.baseMapper.insert(agencyCertificationAudit);
    }

    @Override
    public AgencyCertificationAudit getselectById(Long providerId) {
//        System.out.println(this.baseMapper.selectById(providerId));
        return this.baseMapper.selectById(providerId);
    }


    /**
     * 驳回原因
     * @return
     */
    @Override
    public AgencyCertificationAuditResDTO getReason() {
        AgencyCertificationAudit agencyCertificationAudit =  this.baseMapper.selectById(UserContext.currentUserId());
        return BeanUtil.toBean(agencyCertificationAudit, AgencyCertificationAuditResDTO.class);
    }

    @Override
    public PageResult<AgencyCertificationAuditResDTO> getpage(AgencyCertificationAuditPageQueryReqDTO agencyCertificationAuditPageQueryReqDTO) {
        //1.分页查询用户
        Page<AgencyCertificationAudit> page = PageUtils.parsePageQuery(agencyCertificationAuditPageQueryReqDTO, AgencyCertificationAudit.class);
        LambdaQueryWrapper<AgencyCertificationAudit> queryWrapper = Wrappers.<AgencyCertificationAudit>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(agencyCertificationAuditPageQueryReqDTO.getName()), AgencyCertificationAudit::getName, agencyCertificationAuditPageQueryReqDTO.getName())
                .eq(ObjectUtil.isNotEmpty(agencyCertificationAuditPageQueryReqDTO.getLegalPersonName()), AgencyCertificationAudit::getLegalPersonName, agencyCertificationAuditPageQueryReqDTO.getLegalPersonName())
                .eq(ObjectUtil.isNotEmpty(agencyCertificationAuditPageQueryReqDTO.getCertificationStatus()), AgencyCertificationAudit::getCertificationStatus, agencyCertificationAuditPageQueryReqDTO.getCertificationStatus())
                .eq(ObjectUtil.isNotEmpty(agencyCertificationAuditPageQueryReqDTO.getCertificationStatus()), AgencyCertificationAudit::getCertificationStatus, agencyCertificationAuditPageQueryReqDTO.getCertificationStatus());
        Page<AgencyCertificationAudit> agencyCertificationAuditPage = agencyCertificationAuditMapper.selectPage(page, queryWrapper);

        //3.封装数据
        return PageUtils.toPage(agencyCertificationAuditPage, AgencyCertificationAuditResDTO.class);
    }

    @Override
    public void audit(CertificationAuditReqDTO certificationAuditReqDTO) {
        AgencyCertificationAudit agencyCertificationAudit = new AgencyCertificationAudit();
        BeanUtil.copyProperties(certificationAuditReqDTO, agencyCertificationAudit);
        agencyCertificationAudit.setAuditorId(UserContext.currentUserId());
        agencyCertificationAudit.setAuditorName(UserContext.currentUser().getName());
        agencyCertificationAudit.setAuditTime(LocalDateTime.now());
        agencyCertificationAudit.setAuditStatus(CertificationStatusEnum.PROGRESSING.getStatus());
        this.baseMapper.updateById(agencyCertificationAudit);
    }
}
